package com.corpgovernment.organization.config;

import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import com.ctrip.corp.obt.generic.core.context.RequestContext;
import com.ctrip.corp.obt.generic.utils.HttpUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @author xukang zhang
 * @date 2024/4/28 17:25
 */
@Component
@Slf4j
public class IpCheckInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            /*IpCheck classAnnotation = handlerMethod.getBeanType().getAnnotation(IpCheck.class);
            IpCheck methodAnnotation = handlerMethod.getMethodAnnotation(IpCheck.class);*/
            String ip = HttpUtils.getIp(request);
            log.info("Request's IP: {}, RequestUri: {}", ip, Optional.ofNullable(RequestContext.getCurrentContext())
                .map(RequestContext::getRequestURI).orElse("Unknown"));
            /*if (classAnnotation != null || methodAnnotation != null) {
                String ip = HttpUtils.getIp(request); // 获取请求的IP地址
                log.info("Request's IP: {}", ip);
            }*/
        }
        return true;
    }
}
